package capricornus.net.http
{
	import capricornus.events.LoaderEvent;
	import capricornus.net.loader.SingleUrlLoader;
	import capricornus.utils.MathUtils;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.navigateToURL;
	import flash.utils.Dictionary;

	public class HttpRequestManager
	{
		private static var _dictRequest:Dictionary = new Dictionary();
		
		/**
		 * 执行http请求 
		 * 注：这个跟request（）不同的是，这个方法会有持续信息
		 * @param url
		 * @param callBackFun
		 * @return 
		 * 
		 */		
		public static function requestContinue( url:String, callBackFun:Function=null ):int
		{
			var id:int = MathUtils.getUniqueID();
			_dictRequest[id] = new HttpContinueRequest( id, url, callBackFun );
			return id;
		}
		
		/**
		 * 停止请求
		 * @param requestID requestContinue函数的返回值
		 * 
		 */		
		public static function clearContinue( requestID:int ):void
		{
			var ht:HttpContinueRequest = _dictRequest[requestID];
			if( ht != null )
			{
				delete _dictRequest[requestID];
				ht.destroy();
			}
		}
		
		/**
		 * 执行http请求 
		 * @param url
		 * @param callBackFun 信息返回处理的函数 callBackFun( msg:String ):void;
		 * @return 错误则返回0，否则非0
		 */		
		public static function request( url:String, callBackFun:Function=null ):int
		{
			if( url==null || url=="" ) return 0;
			
			var id:int = MathUtils.getUniqueID();
			_dictRequest[id] = new HttpRequest( id, url, callBackFun );
			
			return id;
		}
		
		/**
		 * 停止请求 
		 * @param requestID request函数的返回值
		 * 
		 */		
		public static function clear( requestID:int ):void
		{
			var ht:HttpRequest = _dictRequest[requestID];
			if( ht != null )
			{
				delete _dictRequest[requestID];
				ht.destroy();
			}
		}
		
		/**
		 * 推送信息 
		 * @param url
		 * 
		 */		
		public static function post( url:String ):void
		{
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.POST;
			navigateToURL( request );
		}
	}
}



